import Http from "../../../utils/http.js";
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    scrollHeight: wx.getSystemInfoSync().windowHeight,
    isScro2Show: false,
    isScro3Show: false,
    firstLetter: "",
    targetLetter: "",
    scro1Info: "",
    scro2Info: "",
    scro3Info: "",
    scro1Id: "",
    scro1Name: "",
    scro2Id: "",
    scro2Name: "",
    scro3Id: "",
    scro3Name: "",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    /**
     * 获取所有的品牌
     */
    Http.request({
      url: '/mall/vehicle/getBrand',
      method: 'GET',
      data: {
        apiType: "baidu",
        loadMore: 0
      },
      success: (res) => {
        this.setData({
          firstLetter: Object.keys(res),
          scro1Info: res

        })
      }
    });
  },
  bindGetCarNameList: function (e) {
    var dataSet = e.currentTarget.dataset;
    console.log('bindGetCarNameList', dataSet);
    wx.showLoading()
    this.setData({
      isScro2Show: false,
      isScro3Show: false,
      scro1Name: dataSet.name,
      scro1Id: dataSet.id
    })
    Http.request({
      url: '/mall/vehicle/getFamily',
      method: 'GET',
      data: {
        apiType: "baidu",
        brandId: dataSet.id
        // baiduBrandId: dataSet.baiduid,
        // loadMore: 0
      },
      success: (res) => {
        wx.hideLoading()
        this.setData({
          scro2Info: res,
          isScro2Show: true
        })
      },
      error:function(){
        wx.hideLoading()
      }
    });
  },

  bindGetCarYearList: function (e) {
    var dataSet = e.currentTarget.dataset;
    console.log(dataSet)
    this.setData({
      scro2Id: dataSet.id,
      scro2Name: dataSet.name,
      isScro3Show: false
    });
    wx.showLoading()
    Http.request({
      url: '/mall/vehicle/getVersion',
      method: 'GET',
      data: {
        apiType: "baidu",
        familyId: dataSet.id,
        // familyId: dataSet.id,
        // baiduFamilyId: dataSet.baiduid,
        // loadMore: 0
      },
      success: (res) => {
        console.log(res)
        wx.hideLoading()
        if (!res.length) {
          this._handlePrevPage();
        } else {
          this.setData({
            isScro3Show: true,
            scro3Info: res
          })
        }
      },
      error:function(){
        wx.hideLoading()
      }
    });
  },

  bindGetCarFinalType: function (e) {
    var dataSet = e.currentTarget.dataset;
    console.log(dataSet)
    this.setData({
      scro3Id: dataSet.id,
      scro3Name: dataSet.name
    });

   this._handlePrevPage();
  },


  _handlePrevPage:function() {
    let pages = getCurrentPages();
    let prevPage = null; //上一个页面
    if (pages.length >= 2) {
      prevPage = pages[pages.length - 2]; //上一个页面
    }
    console.log('汽车品牌',this.data.scro1Name,this.data.scro2Name,this.data.scro3Name)
    prevPage && prevPage.pageCarBrandChooseBrand({
      brand: this.data.scro1Name,
      audi: this.data.scro2Name,
      cartype: this.data.scro3Name,
      //选定id
      modelId:this.data.scro3Id
    });
    wx.navigateBack();
  },



  bindLetterTap: function (e) {
    var dataSet = e.currentTarget.dataset;
    console.log(dataSet.letter)
    this.setData({
      targetLetter: dataSet.letter
    })
  },




})